﻿$(function () {
    $("#focu-grid").datagrid({
        url: $.Api("Gen/Focu/Grid"),
        nowrap: false, rownumbers: true, border: false,
        fit: true, singleSelect: true,
        pageSize: 20, pagination: true,
        idField: "ID", toolbar: "#focu-tools",
        columns:
        [[
            { field: "Name", title: "焦点名称", width: 200, align: "center" },
            {
                field: "Img", title: "焦点图片", width: 120, align: "center",
                formatter: function (value, row, index) {
                    return $.GetIconButton("icon-images", "查看焦点",
                        "Focu.ShowImg(\"" + value + "\",\"" + row.Name + "\",\"" + row.Group + "\")");
                }
            },
            { field: "Group", title: "焦点分组", width: 100, align: "center" },
            { field: "Link", title: "链接地址", width: 280, align: "center" },
            { field: "IntSort", title: "排序", width: 50, align: "center" },
            {
                field: "IsVisible", title: "启用", width: 50, align: "center",
                formatter: function (v, r, i) { return $.GetYesOrNo(v); }
            }
        ]]
    });
});

var Focu = {
    //添加
    Insert: function () {
        Focu.Mutual("添加", "gen/focu/Edit.aspx");
    },
    //修改
    Update: function () {
        var row = $("#focu-grid").datagrid("getSelected");
        if (row) {
            Focu.Mutual("修改", "gen/focu/Edit.aspx?id=" + row.ID);
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },
    //删除
    Delete: function () {
        var row = $("#focu-grid").datagrid("getSelected");
        if (row) {
            $.EasyuiConfirm("确认删除?", function (del) {
                if (del) {
                    $.AjaxMask({
                        url: $.Api("Gen/Focu/Delete"),
                        type: "post", data: { id: row.ID },
                        success: function (result) {
                            result = JSON.parse(result);
                            $.EasyuiSlide(result.Message);
                            if (result.Success) { Focu.Reload(); }
                        }
                    });
                }
            });
        }
        else { $.EasyuiSlide(delnullrowmsg); }
    },
    //实体操作
    Mutual: function (title, href) {
        $.ModelDialog({
            title: title, href: href, width: 275,
            handler: function () {
                $.ModelForm({
                    validate: true, button: $(this),
                    id: "#focu-model",
                    url: $.Api("Gen/Focu/Save"),
                    success: function (result) { Focu.Reload(); }
                });
            },
            onLoad: function () { Focu.FormInit(); }
        });
    },
    //弹窗初始化
    FormInit: function () {
        $("#focu-group").combobox({
            width: 165, editable: false,
            required: true, data: [
                { value: "Home", text: "首页焦点图片" },
                { value: "RightAdv", text: "内容页右侧广告" },
                { value: "ButtonAdv", text: "底部广告横幅" },
                { value: "LeftButton", text: "首页左下角图片" }
            ]
        });
    },

    //排序向上
    Up: function () {
        Focu.Order("up");
    },
    //排序向下
    Down: function () {
        Focu.Order("down");
    },
    //排序操作
    Order: function (order) {
        var row = $("#focu-grid").datagrid("getSelected");
        if (row) {
            $.AjaxMask({
                url: $.Api("Gen/Focu/SetOrder"), type: "post",
                data: { id: row.ID, group: row.Group, order: order },
                success: function (result) {
                    result = JSON.parse(result);
                    $.EasyuiSlide(result.Message);
                    if (result.Success) { Focu.Reload(); }
                }
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //启用
    Enable: function () {
        Focu.EnableOrDisable(true);
    },
    //禁用
    Disable: function () {
        Focu.EnableOrDisable(false);
    },
    //启用禁用
    EnableOrDisable: function (bool) {
        var row = $("#focu-grid").datagrid("getSelected");
        if (row) {
            $.AjaxMask({
                url: $.Api("Gen/Focu/EnableOrDisable"),
                type: "post", data: { id: row.ID, bit: bool },
                success: function (result) {
                    result = JSON.parse(result);
                    $.EasyuiSlide(result.Message);
                    if (result.Success) { Focu.Reload(); }
                }
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //刷新列表
    Reload: function () {
        $.DataGridReload("#focu-grid");
    },

    //显示焦点
    ShowImg: function (file, text, group) {
        if (!file) { text = "查看焦点"; file = $("#focu-img").val(); }
        if (!group) { group = $("#focu-group").combobox("getValue"); }

        var w = 800, h = 480;

        if (file != "null.png") {
            switch (group) {
                case "Home": w = 690, h = 350; break;
                case "RightAdv": w = 249, h = 223; break;
                case "ButtonAdv": w = 1000, h = 100; break;
                case "LeftButton": w = 246, h = 178; break;
            }
        }

        var img = "<img style='display:block; margin:auto;' src='../userfiles/focu/" + file + "' />";
        var box = "<div style='width:" + w + "px;height:" + h + "px;'>" + img + "</div>";

        $.ModelDialog({ title: text, width: w + 14, iconCls: "icon-images", content: box });
    },
    //上传焦点
    AddImg: function () {
        var group = $("#focu-group").combobox("getValue");
        var w = 800, h = 480;
        switch (group) {
            case "Home": w = 690, h = 350; break;
            case "RightAdv": w = 249, h = 223; break;
            case "ButtonAdv": w = 1000, h = 100; break;
            case "LeftButton": w = 246, h = 178; break;
        }
        KindEditor.editor({
            uploadJson: $.Api("Gen/Focu/AddImg&w=" + w + "&h=" + h),
            formatUploadUrl: false
        }).loadPlugin("image", function () {
            this.plugin.imageDialog({
                showRemote: false,
                clickFn: function (url, title, width, height, border, align) {
                    $.EasyuiSlide("上传成功!"); this.hideDialog(); $("#focu-img").val(url);
                }
            });
        });
    },
    //清除焦点
    DeleteImg: function () {
        $("#focu-img").val("null.png"); $.EasyuiSlide("清除成功!");
    }
};